package com.hf.www.base.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.hf.www.base.common.BaseController;
import com.hf.www.base.model.SysMenusModel;
import com.hf.www.core.plugin.annotation.Control;
import com.jfinal.kit.JsonKit;
import com.jfinal.log.Logger;

/**
 * 菜单
 * 
 *
 */
@Control(controllerKey = "/menu")
public class MenuController extends BaseController {

	private static final Logger LOG = Logger.getLogger(MenuController.class);

	
	public void index(){
		setAttr("lMenus"  , SysMenusModel.dao.getMenus(2)) ;
		setAttr("hMenus"  , SysMenusModel.dao.getMenus(1)) ;
		render("system/menus");
	}
	
	
	public void edit(){
		SysMenusModel menu = SysMenusModel.dao.findById(getPara("id"));
		setAttr("data", menu);
		renderJson();
	}
	
	/**
	 * 编辑保存菜单
	 */
	public void save(){
		
		SysMenusModel menu = getModelWithOutModelName(SysMenusModel.class, true);
		
		try {
			SysMenusModel.dao.editSave(menu);
			setAttr("r", true);
			setAttr("d", menu);
		} catch (Exception e) {
			setAttr("r" , false);
			setAttr("m" , "编辑保存菜单失败！ ");
			LOG.error("编辑保存菜单失败！ " , e);
		}
		renderJson();
	}
	
	public void batchDel() {
		String ids = getPara("ids");
		
		try {
			SysMenusModel.dao.batchDel(ids);
			setAttr("r", true);
		} catch (Exception e) {
			setAttr("r" , false);
			setAttr("m" , "删除指定菜单失败！");
		}
		renderJson();
	}

	/**
	 * 获取所有顶级菜单
	 */
	public void getMenus(){
		Map<String, Object> params = getParams("isParent", "type");
		List<SysMenusModel> menus = SysMenusModel.dao.getPMenus(params) ;
		renderJson(menus);
	}
	
	
	/**
	 * 获取指定编号的菜单
	 */
	public void getMenu(){
		
		try {
			SysMenusModel menu = SysMenusModel.dao.findById(getPara("id"));
			setAttr("r", true);
			setAttr("d", menu);
		} catch (Exception e) {
			setAttr("r", false);
			setAttr("m", "加载指定菜单数据失败！");
			LOG.error("加载指定菜单数据失败！", e);
		}
		
		renderJson();
	}
	
	
	
	public void resolveIcon(){
		List<Map<String, Object>> result  = new ArrayList<Map<String,Object>>();
		
		try {
			Document doc = Jsoup.parse(new File("E://WorkSpace/iconlist.html"), "UTF-8");
			
			Elements list = doc.select(".doc-example > section");
			for (Element sec : list) {
				Map<String, Object> iconMap = new HashMap<String, Object>();
				List<String> il = new ArrayList<String>();
				iconMap.put("id", sec.attr("id"));
				iconMap.put("title", sec.select(">h2").text());
				
				Element ul = sec.select(">ul").get(0); 
				
				Elements lis = ul.children();
				System.out.println(lis.size());
				for (Element li : lis) {
					Element i = li.child(0).child(0);
					System.out.println(i.attr("class"));
					il.add(i.attr("class"));
				}
				
				iconMap.put("list", il);
				result.add(iconMap);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		renderJson(result);
	}
	
	
	public static void main(String[] args) {
		
		
	}
}
